<?php

/**
 * 项目的计划任务
 */
class CronjobAction extends Action
{

    /**
     * 任务执行间隔
     * @var interget 
     */
    protected $limit = 30;

    /**
     * 定时器
     */
    function index() {

        /**
         * 设置后台运行
         */
        ignore_user_abort(1);

        /**
         * 取消脚本超时
         * @param interger 0为不限制超时时间
         */
        set_time_limit(0);
        do {

            /**
             * 执行任务
             */
            $this->doTask();
            /**
             * 30秒钟执行一次
             */
            sleep($this->limit);
        } while (true);
    }

    /**
     * 设置执行任务
     * return boolen
     */
    public function doTask() {
        $this->sendEmail();
    }

    public function sendEmail() {
        $model = M('EmailTaskList');
        $map['next_send_at'] = array('lt', time());

        $emails = $model
                ->where($map)
                ->limit(5)
                ->select();

        $templateModel = M('EmailTemplate');
        //1371138916

        if ($emails) {
            foreach ($emails as $email) {
                /**
                 * 获取邮件模板
                 */
                $template = $templateModel->where('template_id = ' . $email['template_id'])->find();
                /**
                 * 发送邮件
                 */
                $rs = $this->phpMail($email['email'], $template['title'], $template['content'], $template['title']);
                /**
                 * 修改任务的状态，设置下一次发送时间，统计发送次数
                 */
                if($rs) {
                    $data['last_send_status'] = '1';
                    $data['last_send_at'] = time();
                    $data['next_send_at'] = $email['next_send_at'] + 3600 * 24 * $email['interval'];
                    $data['send_count'] = $email['send_count'] + 1;
                    $model->where('id = ' . $email['id'])->data($data)->save();
                    debug('send email to ' . $email['email']);
                }
                
            }
        }
    }

    public function phpMail($to, $subject = '', $body = '', $altBody = "") {
        $model = M('EmailConfig');
        $config = $model->where('status = 1')->order('sort asc')->find();

        vendor('PHPMailer.class#phpmailer');
        $mail = new PHPMailer(true); //New instance, with exceptions enabled
        $mail->IsSMTP();                           // tell the class to use SMTP
        $mail->SMTPAuth = true;                  // enable SMTP authentication
        $mail->Port = $config['smtp_port'];                    // 25
        $mail->Host = $config['smtp'];   // localhost
        $mail->Username = $config['email'];      // support
        $mail->Password = $config['password'];            // lin12345
        $mail->CharSet = "UTF-8";
        //$mail->IsSendmail();  // tell the class to use Sendmail
        $mail->SMTPDebug = false;
        $mail->AddReplyTo($config['email'], $config['email_name']);
        $mail->From = $config['email']; //service@sharesdk.cn
        $mail->FromName = $config['email_name'];
        $mail->AddAddress($to, 'Hi');
        $mail->Subject = $subject;
        $mail->AltBody = $altBody; // optional, comment out and test
        $mail->WordWrap = 80; // set word wrap

        $mail->MsgHTML($body);
        $mail->IsHTML(true); // send as HTML
        return $mail->send();

    }

}

?>
